package com.hqyj.service.impl;

import com.hqyj.service.LoginService;
import com.hqyj.utile.EmailUtile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Random;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    EmailUtile emailUtile;

    /**
     * 发送验证码
     * @param email 邮箱
     * @param request
     * @return
     */
    public HashMap<String, Object> sendCode(String email, HttpServletRequest request) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //生成随机数
        Random rd = new Random();
        int code = rd.nextInt(1000);
        //把验证码存入到session中
        HttpSession session = request.getSession();
        session.setAttribute("code",code);

        boolean is = emailUtile.sead(email,"你的验证码是"+code,"酒店管理的验证码");
        if(is){
            map.put("info","发送成功");
        }else{
            map.put("info","发送失败");
        }
        return map;
    }

    public HashMap<String, Object> loginEmail(String code, HttpServletRequest request) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        HttpSession session = request.getSession();
        String sCode = session.getAttribute("code")+"" ;
        if(sCode.equals(code)){
            map.put("info","登录成功");
        }else{
            map.put("info","登录失败");
        }
        return map;
    }
}
